import Peer from 'simple-peer'

let peer = null;

export const init = (stream, initiator) => {
    peer = new Peer({
        initiator: initiator,
        stream: stream,
        trickle: false,
        reconnectTimer: 1000,
        iceTransportPolicy: 'relay',
        config: {
            iceServers: [
                { urls: process.env.REACT_APP_STUN_SERVERS.split(',') },
                {
                    urls: process.env.REACT_APP_TURN_SERVERS.split(','),
                    username: process.env.REACT_APP_TURN_USERNAME,
                    credential: process.env.REACT_APP_TURN_CREDENCIAL
                },
            ]
        }
    })
    return peer;
}

export const connect = (otherId) => {
    if (peer) {
        peer.signal(otherId);
    }
}